import GameManager from "./GameManager";

export default class Ball extends Laya.Script {

    constructor() { 
        super(); 
        this.rig=null;
        this.gameManager=null;
        // 更多参数说明请访问: https://ldc2.layabox.com/doc/?nav=zh-as-2-4-0
    }
    onAwake(){
        this.gameManager=this.owner.parent.getComponent(GameManager)
        this.rig=this.owner.getComponent(Laya.RigidBody)
        Laya.stage.on("StarGame",this,this.StartGame)
        Laya.stage.on("GameOver",this,function(){
            this.rig.type="static"
            Laya.timer.scale=0;
            Laya.SoundManager.setSoundVolume(0,"res1/di.wav");
            Laya.SoundManager.setSoundVolume(0,"res1/shibai.mp3");
            Laya.SoundManager.setSoundVolume(0,"res1/sheng.mp3");
            Laya.SoundManager.setSoundVolume(0,"res1/kai.WAV");
            

        })
    }
    onDestroy(){
        Laya.stage.off("StarGame",this,this.StartGame)
        Laya.stage.off("GameOver",this)
    }
    StartGame(){
        if(this.gameManager.isStartGame)
        this.rig.type="dynamic"

    }
    
    reset(x){
        this.owner.x=x;
        this.owner.y=360
        this.rig.setVelocity({x:0,y:0})
        this.rig.angularVelocity=0;
        this.rig.type="static"

    }

    onDisable() {
    }
    onTriggerEnter(other){
        if(this.gameManager.gamerOver){
            this.rig.type="static"
            return;}
        if(other.owner.name=="dimian"){
            Laya.SoundManager.playSound("res1/di.wav",1)
            if(this.owner.x<962){
                Laya.SoundManager.playSound("res1/shibai.mp3",1,
                new Laya.Handler(this,function() {
                    if(this.gameManager.gamerOver){
                        this.rig.type="static"
                        return;}
                    Laya.SoundManager.playSound("res1/kai.WAV",1,
                    new Laya.Handler(this,function () {
                        Laya.stage.event("StarGame")
                    }));
                }))
                this.rig.type="static"
                this.reset(770);    
                Laya.stage.event("ResetMyPlayer")
            }
            else{
                Laya.SoundManager.playSound("res1/sheng.mp3",1,
            new Laya.Handler(this,function() {
                if(this.gameManager.gamerOver){
                    this.rig.type="static"
                    return;}
                Laya.SoundManager.playSound("res1/kai.WAV",1,
                new Laya.Handler(this,function () {
                    Laya.stage.event("StarGame")
                }));
            }))
            this.rig.type="static"
                this.reset(1150);
                Laya.stage.event("ResetAIPlayer")
            }

        }
        if(other.owner.name=="myPlaey"){
            Laya.SoundManager.playSound("res1/My.WAV")
        }
        if(other.owner.name=="AI"){
            Laya.SoundManager.playSound("res1/AI.mp3")
        }
        if(other.owner.name=="pole"){
            Laya.SoundManager.playSound("res1/gan.WAV")
        }

    }
}